package com.wws.family.utils

import android.text.TextUtils
import android.util.Log
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale


/**
 *@Description:时间工具
 *@Date: 2024/9/13 9:24
 *@Author: WangWeiShuo
 */
object DateUtils {
    val YYYYMMDD = "yyyy-MM-dd"

    //获取当前完整的日期和时间
    fun getNowDateTime(): String? {
        val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        return sdf.format(Date())
    }

    fun getNowDate(): String? {
        val sdf = SimpleDateFormat("yyyy-MM-dd")
        return sdf.format(Date())
    }

    fun getNowDateTimeForLong(): Long? {
        return Date().time
    }

    fun getNowDateNoHeng(): String? {
        val sdf = SimpleDateFormat("yyyyMMdd")
        return sdf.format(Date())
    }

    //获取当前时间
    fun getNowTime(): String? {
        val sdf = SimpleDateFormat("HH:mm:ss")
        return sdf.format(Date())
    }

    //获取当前时间（精确到毫秒）
    fun getNowTimeDetail(): String? {
        val sdf = SimpleDateFormat("HH:mm:ss.SSS")
        return sdf.format(Date())
    }

    fun getBetweenTime(startTime: String?,stopTime: String?): Long{
//        val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        var dateFormat = SimpleDateFormat("yyyy-MM-dd")

        val sTime: Date = dateFormat.parse(startTime)
        val eTime: Date = dateFormat.parse(stopTime)
//        var eTime: Date = Calendar.getInstance().time//系统现在时间
        println("eTime=$eTime")
        val diff = eTime.time - sTime.time
        val days = diff / (1000 * 60 * 60 * 24)
//        val hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
//        val minutes = ((diff - days * (1000 * 3600 * 24)) - hours * (1000 * 3600)) / (1000 * 60)
//        val second = (diff - days * 1000 * 3600 * 24 - hours * 1000 * 3600 - minutes * 1000 * 60) / 1000
        return days
    }

    fun obtainWeek(dateTime: String?, formatDate: String?): String? {
        var week = ""
        val calendar: Calendar = Calendar.getInstance()
        //如果日期为空，计算当天是周几
        if (TextUtils.isEmpty(dateTime)) {
            calendar.setTime(Date(System.currentTimeMillis()))
        } else {
            val sdf = SimpleDateFormat(formatDate, Locale.getDefault())
            var date: Date? = null
            try {
                date = sdf.parse(dateTime)
            } catch (e: Exception) {
                Log.d("callback", "时间解析异常")
            }
            if (date != null) {
                calendar.setTime(Date(date.time))
            }
        }
        when (calendar.get(Calendar.DAY_OF_WEEK)) {
            1 -> week = "周日"
            2 -> week = "周一"
            3 -> week = "周二"
            4 -> week = "周三"
            5 -> week = "周四"
            6 -> week = "周五"
            7 -> week = "周六"
        }
        return week
    }

    fun getCurrentYear(): Int {
        val calendar = Calendar.getInstance()
        return calendar[Calendar.YEAR]
    }

    fun getCurrentMonth(): Int {
        // 获取 Calendar 实例
        val calendar = Calendar.getInstance()
        // Calendar 类中月份是从 0 开始计数的，所以获取的值需要加 1 才是实际的月份
        return calendar[Calendar.MONTH] + 1
    }
}